<?php
/*
 *      user.php
 *      
 *      Copyright 2011 Long Q Tran <tqlong@tqlong-terrell>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *      
 *      
 */

function check_username_password($username, $password) {
	$pass_sha1 = sha1($password);
	$result = mysql_query("SELECT * FROM user WHERE username = '$username';");
	$error = mysql_errno();
	$ret = 0;
	if ($error) {
		echo "Error while checking user: ", mysql_error(), "<br />";
		$ret = 3;
	}
	if ($result && mysql_num_rows($result)) {
		$row = mysql_fetch_assoc($result);
		if ($row["password"] == $pass_sha1) {
			$ret = 1; // user & pass correct
		}
		else {
			$ret = 2; // user found but pass incorrect
		}
	}
	else {
		$ret = 0; // user not found
	}
	mysql_free_result($result);
	return $ret;
}

function add_user($username, $password) {
	$pass_sha1 = sha1($password);
	mysql_query("INSERT INTO user (username, password) VALUES ('$username','$pass_sha1');");
	$error = mysql_errno();
	if ($error) {
		echo "Error while adding user: ", mysql_error(), "<br />";
		return 0; // error
	}
	else {
		return 1; // ok
	}
}

?>
